package com.swz.dcrm.ui.viewmodel;

import android.view.View;

import androidx.lifecycle.MediatorLiveData;

import com.flyco.dialog.widget.ActionSheetDialog;
import com.swz.dcrm.api.DcrmApi;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Dictionary;
import com.swz.dcrm.model.Edit;
import com.swz.dcrm.model.Input;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.model.PageResponse;
import com.swz.dcrm.model.PersonalInfo;
import com.swz.dcrm.model.Role;
import com.swz.dcrm.model.Token;
import com.xh.baselibrary.callback.BaseViewModel;
import com.xh.baselibrary.callback.CallBack;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.http.Multipart;

public class MainViewModel extends BaseViewModel {
    private Retrofit mRetrofit;
    private MediatorLiveData<Boolean> showTab=new MediatorLiveData<>();
    private MediatorLiveData<Integer> pageIndex=new MediatorLiveData<>();
    private MediatorLiveData<BaseResponse<Token>> loginResult;
    private MediatorLiveData<String> call=new MediatorLiveData<>();
    private MediatorLiveData<BaseResponse<PersonalInfo>> personalInfo;
    private MediatorLiveData<PageResponse<PersonalInfo>> addressBook;
    private MediatorLiveData<Input> input=new MediatorLiveData<>();
    private MediatorLiveData<BaseResponse<List<Role>>>roles;
    private MediatorLiveData<BaseResponse<String>>headUrl;
    private MediatorLiveData<Edit>edit;
    private MediatorLiveData<String>headPic;


    private MediatorLiveData<BaseResponse<List<Dictionary>>>dictionary;
    private DcrmApi dcrmApi;
    public MainViewModel(Retrofit retrofit){
        mRetrofit=retrofit;
        dcrmApi=mRetrofit.create(DcrmApi.class);
    }

    public MediatorLiveData<BaseResponse<List<Dictionary>>> getDictionary(String code){
        dictionary=creatLiveData(dictionary);
        dcrmApi.findDictionaryByTypeCode(code).enqueue(new CallBack<>(this,dictionary));
        return dictionary;
    }
    public void login(String account, String pwd){
        loginResult=creatLiveData(loginResult);
        dcrmApi.login(account,pwd).enqueue(new CallBack<>(this,loginResult));
    }

    public MediatorLiveData<BaseResponse<Token>> getLoginResult() {
        loginResult=creatLiveData(loginResult);
        return loginResult;
    }
    public MediatorLiveData<BaseResponse<PersonalInfo>>getPersonalInfo(){
        personalInfo=creatLiveData(personalInfo);
        dcrmApi.getPersonalInfo().enqueue(new CallBack<>(this,personalInfo));
        return personalInfo;
    }
    public MediatorLiveData<PageResponse<PersonalInfo>> getAddressBook(){
        addressBook=creatLiveData(addressBook);
        dcrmApi.getAddressBook(1,1000)
                .enqueue(new CallBack<>(this,addressBook));
        return addressBook;
    }

    public MediatorLiveData<BaseResponse<List<Role>>> getRoles() {
        roles=creatLiveData(roles);
        dcrmApi.getRole().enqueue(new CallBack<>(this,roles));
        return roles;
    }
    public MediatorLiveData<BaseResponse<String>>upload(File file){
        headUrl=creatLiveData(headUrl);
        RequestBody requestBody=RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part part=MultipartBody.Part.createFormData("file",file.getName(),requestBody);
        dcrmApi.uploadOssFile("jpg",part).enqueue(new CallBack<>(MainViewModel.this,headUrl));
        return headUrl;
    }

    public MediatorLiveData<Edit> getEdit() {
        edit=creatLiveData(edit);
        return edit;
    }


    public boolean isManager(){
        if(personalInfo.getValue()==null){
            return false;
        }
        return personalInfo.getValue().getData().getRole().isManager();
    }

    public MediatorLiveData<String> getHeadPic() {
        headPic=creatLiveData(headPic);
        return headPic;
    }

    public MediatorLiveData<String> getCall() {
        return call;
    }

    public MediatorLiveData<Integer> getPageIndex() {
        return pageIndex;
    }

    public MediatorLiveData<Boolean> getShowTab() {
        return showTab;
    }

    public MediatorLiveData<Input> getInput() {
        return input;
    }
}
